package com.controller;

import com.pojo.Permission;
import com.pojo.SysUser;
import com.service.PermissionService;
import com.service.UserService;
import com.util.TreeUtil;
import com.vo.MenuNode;
import com.vo.PermissionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mornd
 * @date 2020/11/13 - 14:13
 */
@RestController
@RequestMapping("/menu")
public class LeftMenuController {
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private UserService userService;

    @RequestMapping("/loadIndexMenuLeft")
    public Map<String, Object> loadIndexMenuLeft(PermissionVo permissionVO, Principal principal) {
        //创建Map集合，保存MenuInfo菜单信息
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        //创建Map集合，保存homeInfo信息
        Map<String, Object> homeInfo = new LinkedHashMap<String, Object>();
        //创建Map集合，保存logoInfo信息
        Map<String, Object> logoInfo = new LinkedHashMap<String, Object>();
        //设置只查询菜单
        List<MenuNode> menuNodeList = new ArrayList<MenuNode>();
        //获得当前登录用户的id
        SysUser loginUser = userService.findUserInfoByUserName(principal.getName());
        for (Permission permission : permissionService.findPermissionListByUserId(loginUser.getId())) {
            MenuNode menuNode = new MenuNode();
            BeanUtils.copyProperties(permission, menuNode);
            menuNodeList.add(menuNode);
        }
        //保存homeInfo信息
        homeInfo.put("title", "首页");
        homeInfo.put("href", "/toMain");
        //保存logo信息
        logoInfo.put("title", "酒店管理系统");
        logoInfo.put("image", "/statics/layui/images/logo.png");
        logoInfo.put("href", "/toHome");
        //保存菜单menuInfo信息
        map.put("menuInfo", TreeUtil.toTree(menuNodeList, 0));
        map.put("homeInfo", homeInfo);
        map.put("logoInfo", logoInfo);
        return map;
    }
}
